package com.teamclub.domain.erp.dealer;

import io.ebean.Ebean;
import io.ebean.Model;
import com.teamclub.domain.system.AppDomain;
import com.teamclub.domain.system.AppDomainInstance;
import com.teamclub.domain.system.AppOrgArch;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * 厂房和经销商的关联关系
 * Created by ilkkzm on 17-9-17.
 */
@Entity
@Table(name = "erp_dealer_info_link")
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class DealerInfoLink extends Model {
    @Id
    private Integer id;
    private String producerOrganNo;
    private Integer producerArchId;
    private String dealerOrganNo;

    @Transient
    private String dealerOrganName;

    @Transient
    private String producerArchName;

    @Transient
    private String producerOrganName;

    public String getProducerOrganName() {
        AppDomainInstance instance = Ebean.find(AppDomainInstance.class).where().eq("organNo", producerOrganNo).findOne();
        return instance == null ? "" : instance.getName();
    }

    public String getDealerOrganName() {
        AppDomainInstance instance = Ebean.find(AppDomainInstance.class).where().eq("organNo", dealerOrganNo).findOne();
        return  instance == null ? "" : instance.getName();
    }

    public String getProducerArchName() {
        if(this.producerArchId == null) return "" ;
        AppOrgArch instance = Ebean.find(AppOrgArch.class, producerArchId);
        return instance == null ? "" : instance.getArchName();
    }
}
